How to set a Printer Port in C# on a specified Printer

58

How to set a Printer Port in C# on a specified Printer -

public static void SetPrinterPort(string printerName, string portName)
        {
            var oManagementScope = new ManagementScope(ManagementPath.DefaultPath);
            oManagementScope.Connect();

            SelectQuery oSelectQuery = new SelectQuery();
            oSelectQuery.QueryString = @"SELECT * FROM Win32_Printer 
            WHERE Name = '" + printerName.Replace("\\", "\\\\") + "'";

            ManagementObjectSearcher oObjectSearcher =
               new ManagementObjectSearcher(oManagementScope, @oSelectQuery);
            ManagementObjectCollection oObjectCollection = oObjectSearcher.Get();

            foreach (ManagementObject oItem in oObjectCollection)
            {
                oItem.Properties["PortName"].Value = portName;
                oItem.Put();
            }
        }

Comments

Submit
0 Comments